import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'messages_all.dart';

class DemoLocalizations {
  static Future<DemoLocalizations> load(Locale locale) {
    final String name = locale.countryCode?.isEmpty == true ? locale.languageCode : locale.toString();
    final String localeName = Intl.canonicalizedLocale(name);

    return initializeMessages(localeName).then((b) {
      Intl.defaultLocale = localeName;
      return DemoLocalizations();
    });
  }

  static DemoLocalizations? of(BuildContext context) {
    return Localizations.of<DemoLocalizations>(context, DemoLocalizations);
  }

  String get title {
    return Intl.message(
      'Flutter App',
      name: 'title',
      desc: 'Title for the Demo application',
    );
  }

  String get home {
    return Intl.message(
      'Home page',
      name: 'home',
      desc: 'Title for the home page',
    );
  }

  String get login {
    return Intl.message(
      'Login',
      name: 'login',
      desc: 'Title for the login button',
    );
  }

  String get theme {
    return Intl.message(
      'theme',
      name: 'theme',
    );
  }

  String get language {
    return Intl.message(
      'language',
      name: 'language',
    );
  }

  String get logout {
    return Intl.message(
      'logout',
      name: 'logout',
    );
  }

  String get logoutTip {
    return Intl.message(
      'Are you sure you want to log out?',
      name: 'logoutTip',
    );
  }

  String get cancel {
    return Intl.message(
      'cancel',
      name: 'cancel',
    );
  }

  String get yes {
    return Intl.message(
      'yes',
      name: 'yes',
    );
  }

  String get noDescription {
    return Intl.message(
        'There is no description.',
        name: 'noDescription',
    );
  }

  String get userName {
    return Intl.message(
      'UserName',
      name: 'userName',
    );
  }
  String get userNameRequired {
    return Intl.message(
      'Username can not be empty',
      name: 'userNameRequired',
    );
  }
  String get password {
    return Intl.message(
      'Password',
      name: 'password',
    );
  }
  String get passwordRequired {
    return Intl.message(
      'Password can not be empty',
      name: 'passwordRequired',
    );
  }
  String get userNameOrPasswordWrong {
    return Intl.message(
      'Wrong user name or password',
      name: 'userNameOrPasswordWrong',
    );
  }
  String get auto {
    return Intl.message(
      'Auto',
      name: 'auto',
    );
  }


  remainingEmailsMessage(int howMany) => Intl.plural(howMany,
    zero: 'There are no emails left',
    one: 'There is $howMany email left',
    other: 'There are $howMany emails left',
    name: 'remainingEmailsMessage',
    args: [howMany],
    desc: "How many emails remain after archiving.",
    examples: const { 'howMany': 42, 'userName': 'Fred' }
  );
}

// Locale 代理类
class DemoLocalizationsDelegate extends LocalizationsDelegate<DemoLocalizations> {
  const DemoLocalizationsDelegate();

  // 是否支持某个Locale
  @override
  bool isSupported(Locale locale) => ['en', 'zh'].contains(locale.languageCode);

  // Flutter会调用此类加载相应的Locale资源类
  @override
  Future<DemoLocalizations> load(Locale locale) {
    return DemoLocalizations.load(locale);
  }

  // 当Localizations Widget重新build时，是否调用load重新家长Locale资源。
  @override
  bool shouldReload(covariant LocalizationsDelegate<DemoLocalizations> old) => false;

}





















